package ar.com.coonocer.joy.mdg.templatehelpers;

import java.util.List;

import ar.com.coonocer.joy.mdg.xmi.core.Association;
import ar.com.coonocer.joy.mdg.xmi.core.AssociationEnd;
import ar.com.coonocer.joy.mdg.xmi.core.Attribute;
import ar.com.coonocer.joy.mdg.xmi.core.ModelElement;
import ar.com.coonocer.joy.mdg.xmi.datatype.Multiplicity;
import ar.com.coonocer.joy.mdg.xmi.model.Package;

public class AttributeUtil {
	//Recupera lower para el atributo teniendo en cuenta posibles asociaciones
	static public Integer getLowerMultiplicity(Package package_, ModelElement clazz, Attribute att) {
		Integer lower = null;
		//
		// Busca multiplicity del atributo
		Multiplicity multiplicity = att.getMultiplicity();
		lower = MultiplicityUtil.getLower(multiplicity);
		//
		// Busca multiplicity en asociaciones
		List<Association> associationsForAttribute = AssociationUtil.getAssociationsFromAttribute(package_, clazz, att);
		for (Association association : associationsForAttribute) {
			List<AssociationEnd> ends = association.getConnection();
			if (ends != null && ends.size() >= 2) {
				if (ends.get(1) != null) {
					multiplicity = ends.get(1).getMultiplicity();
					//System.err.println("getLowerMultiplicity " + clazz.getName() + "." + att.getName() + " " + lower + " -> " + getLower(multiplicity));
					lower = MultiplicityUtil.getLower(multiplicity);
				}
			}
		}
		//
		return lower;
	}

	//Recupera upper para el atributo teniendo en cuenta posibles asociaciones
	static public String getUpperMultiplicity(Package package_, ModelElement clazz, Attribute att) {
		String upper = null;
		//
		// Busca multiplicity del atributo
		Multiplicity multiplicity = att.getMultiplicity();
		upper = MultiplicityUtil.getUpper(multiplicity);
		//
		// Busca multiplicity en asociaciones
		List<Association> associationsForAttribute = AssociationUtil.getAssociationsFromAttribute(package_, clazz, att);
		for (Association association : associationsForAttribute) {
			List<AssociationEnd> ends = association.getConnection();
			if (ends != null && ends.size() >= 2) {
				if (ends.get(1) != null) {
					multiplicity = ends.get(1).getMultiplicity();
					//System.err.println("getUpperMultiplicity " + clazz.getName() + "." + att.getName() + " " + upper + " -> " + getUpper(multiplicity));
					upper = MultiplicityUtil.getUpper(multiplicity);
				}
			}
		}
		//
		return upper;
	}

}
